package com.example.downloadmanager;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.service.notification.Condition;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getCanonicalName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_download).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText etUrl =findViewById(R.id.et_url);
                final String url = etUrl.getText().toString();
                if(url.trim().length()==0){
                    return;
                }
                // 取文件名 http://www.baidu.com/A/B/C/my.jpg   ==>my.jpg(文件名)
                String fileName =url.substring(url.lastIndexOf("/")+1);

                Uri uri=Uri.parse(url);
                DownloadManager.Request request= new DownloadManager.Request(uri);
                request.setTitle("下载文件"+fileName);
                request.setDescription("文件路径"+url);
                request.setDestinationInExternalFilesDir(v.getContext(), Environment.DIRECTORY_DOWNLOADS,fileName);
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

                DownloadManager downloadManager =null;
                if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
                    downloadManager =getSystemService(DownloadManager.class);
                }else{
                    downloadManager=(DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);

                }
                final long id = downloadManager.enqueue(request);

                IntentFilter filter =new IntentFilter();
                filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
                filter.addAction(DownloadManager.ACTION_NOTIFICATION_CLICKED);
                final DownloadManager finalDownloadManager =downloadManager;
                BroadcastReceiver receiver =new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        String action = intent.getAction();
                        long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1);
                        Uri uri = finalDownloadManager.getUriForDownloadedFile(downloadId);
                        if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)){
                            Log.i (TAG,"onReceive:文件下载完成");
                        }else if(DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)){
                            Log.i(TAG,"OnReceie:取消下载"+uri);
                            finalDownloadManager.remove(downloadId);
                        }

                    }
                };
                registerReceiver(receiver,filter);






            }
        });
    }
}